﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Coleotrack.Areas.Admin.Models;
using Coleotrack.Data;
using Coleotrack.Models;
using Coleotrack.Service;
using Coleotrack.Service.Contracts;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;

namespace Coleotrack.Areas.Admin.Controllers
{
    public class ProjectController : Controller
    {
        public IProjectService ProjectService
        {
            get; private set;
        }

        public IUserService UserService
        {
            get;
            private set;
        }

        public ProjectController(IProjectService projectService, IUserService userService)
        {
            ProjectService = projectService;
            UserService = userService;
        }

        [HttpGet, Authorize]
        public ActionResult Index()
        {
            var all = ProjectService.All();

            if (this.Request.IsAjaxRequest())
            {
                return View("_List", all);
            }
            else
            {
                return View(all);
            }
        }

        [HttpGet, Authorize]
        public ActionResult Create()
        {
            var model = new ProjectViewModel();
            model.Users = UserService.Select().ToArray();

            return View(model);
        }

        [HttpPost, Authorize]
        public ActionResult Create(Project project, int contactPerson)
        {
            project.ContactPerson = UserService.Find(contactPerson);
            if (!ValidateProject(project))
            {
                var model = new ProjectViewModel();
                model.Project = project;
                return View(model);
            }

            try
            {
                ProjectService.Insert(project);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View("Error");
            }
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            var model = new ProjectViewModel();
            model.Project = ProjectService.Find(id);
            model.Users = UserService.Select().ToArray();

            return View(model);
        }

        [HttpPost, Authorize]
        public ActionResult Edit(FormCollection collection, int id, int contactPerson)
        {
            Project project = ProjectService.Find(id);
            UpdateModel<Project>(project, "Project");

            project.ContactPerson = UserService.Find(contactPerson);
            if (!ValidateProject(project))
            {
                ProjectService.Evict(project);
                return View();
            }
            
            return RedirectToAction("Index");
        }

        [HttpPost, Authorize]
        public ActionResult Delete(ICollection<int> id)
        {
            foreach (var deleteId in id)
            {
                ProjectService.Delete(ProjectService.Find(deleteId));
            }

            return RedirectToAction("Index");
        }

        private bool ValidateProject(Project project)
        {
            if (!project.IsValid())
            {
                foreach (var ruleViolation in project.GetRuleViolations())
                {
                    ModelState.AddModelError(ruleViolation.PropertyName, ruleViolation.ErrorMessage);
                }
            }

            if (ProjectService.Select().Any(x => x.Abbreviation.Equals(project.Abbreviation) && x.Id != project.Id))
            {
                ModelState.AddModelError("Abbreviation", "Project with the same abbreviation already exists.");
            }

            if (ProjectService.Select().Any(x => x.Name.Equals(project.Name) && x.Id != project.Id))
            {
                ModelState.AddModelError("Name", "Project with the same name already exists.");
            }

            return ModelState.IsValid;
        }
    }
}
